package com.example.distillapplication.ui.fragment;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.example.distillapplication.R;
import com.example.distillapplication.databinding.FragmentHomeBinding;
import com.example.distillapplication.ui.fragment.base.BaseFragment;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment {
    FragmentHomeBinding fragmentHomeBinding;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        fragmentHomeBinding = FragmentHomeBinding.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //进入页面设置按键状态
        fragmentHomeBinding.addwaterswitch.setChecked(mInstrumentStateViewModel.addWaterSwitchState.getValue());
        fragmentHomeBinding.valveswitch.setChecked(mInstrumentStateViewModel.valveSwitchState.getValue());
        fragmentHomeBinding.lightswitch.setChecked(mInstrumentStateViewModel.lightSwitchState.getValue());
        fragmentHomeBinding.heatswitch.setChecked(mInstrumentStateViewModel.heatSwitchState.getValue());
        fragmentHomeBinding.coldswitch.setChecked(mInstrumentStateViewModel.refrigerationSwitchState.getValue());
        fragmentHomeBinding.coldwaterswitch.setChecked(mInstrumentStateViewModel.addCoolantSwitchState.getValue());
        fragmentHomeBinding.sodaswitch.setChecked(mInstrumentStateViewModel.sodaSwitchState.getValue());
        if(!mInstrumentStateViewModel.securityDoorClosed.getValue()){
            fragmentHomeBinding.safetydoor.setImageResource(R.drawable.safetydooropen);
        }else {
            fragmentHomeBinding.safetydoor.setImageResource(R.drawable.safetydoor);
        }



        //加水
        fragmentHomeBinding.addwaterswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mInstrumentStateViewModel.addWaterSwitchState.postValue(isChecked);
            }
        });

        mInstrumentStateViewModel.addWaterSwitchState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                fragmentHomeBinding.addwaterswitch.setChecked(aBoolean);
            }
        });

        //开阀
        fragmentHomeBinding.valveswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mInstrumentStateViewModel.valveSwitchState.postValue(isChecked);
            }
        });

        mInstrumentStateViewModel.valveSwitchState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                fragmentHomeBinding.valveswitch.setChecked(aBoolean);
            }
        });

        //开灯
        fragmentHomeBinding.lightswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mInstrumentStateViewModel.lightSwitchState.postValue(isChecked);
            }
        });

        mInstrumentStateViewModel.lightSwitchState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                fragmentHomeBinding.lightswitch.setChecked(aBoolean);
            }
        });

        //加热
        fragmentHomeBinding.heatswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mInstrumentStateViewModel.heatSwitchState.postValue(isChecked);
            }
        });

        mInstrumentStateViewModel.heatSwitchState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                fragmentHomeBinding.heatswitch.setChecked(aBoolean);
            }
        });

        //制冷
        fragmentHomeBinding.coldswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mInstrumentStateViewModel.refrigerationSwitchState.postValue(isChecked);
            }
        });

        mInstrumentStateViewModel.refrigerationSwitchState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                fragmentHomeBinding.coldswitch.setChecked(aBoolean);
            }
        });

        //加制冷液
        fragmentHomeBinding.coldwaterswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mInstrumentStateViewModel.addCoolantSwitchState.postValue(isChecked);
            }
        });

        mInstrumentStateViewModel.addCoolantSwitchState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                fragmentHomeBinding.coldwaterswitch.setChecked(aBoolean);
            }
        });

        //加碱
        fragmentHomeBinding.sodaswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mInstrumentStateViewModel.sodaSwitchState.postValue(isChecked);
            }
        });

        mInstrumentStateViewModel.sodaSwitchState.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                fragmentHomeBinding.sodaswitch.setChecked(aBoolean);
            }
        });

        //安全门
        mInstrumentStateViewModel.securityDoorClosed.observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(!aBoolean){
                    fragmentHomeBinding.safetydoor.setImageResource(R.drawable.safetydooropen);
                }else {
                    fragmentHomeBinding.safetydoor.setImageResource(R.drawable.safetydoor);
                }
            }
        });

        //水位
        mInstrumentStateViewModel.waterLevel.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                fragmentHomeBinding.waterlevel.setText(String.valueOf(integer));
            }
        });

        //蒸馏量
        mInstrumentStateViewModel.steamVolume.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                fragmentHomeBinding.steamvolume.setText(String.valueOf(integer));
            }
        });

        //温度
        mInstrumentStateViewModel.temperature.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                fragmentHomeBinding.temp.setText(String.valueOf(integer));
            }
        });
    }


}
